package org.example.ttms.entityClass;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.MapType;

import java.io.*;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Scanner;

public class Account {
    private String account;
    private String password;
    private String type;
    private String id;

    public Account() {
    }

    public Account(String account, String password, String type, String id) {
        this.account = account;
        this.password = password;
        this.type = type;
        this.id = id;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Account{" +
                "account='" + account + '\'' +
                ", password='" + password + '\'' +
                ", type='" + type + '\'' +
                '}';
    }
    //获取所有账户
    public HashMap<String, Account> obtainAllAccount(){
        HashMap<String, Account> hashMap = new HashMap<>();
        try {
            String file = "src/main/java/org/example/ttms/file/account.txt";
            Scanner scan = new Scanner(new FileReader(file));
            while(scan.hasNextLine()){
                String str = scan.nextLine();
                ObjectMapper objectMapper = new ObjectMapper();
                Account account1 = objectMapper.readValue(str, Account.class);
                hashMap.put(account1.account, account1);
            }
        } catch (FileNotFoundException | JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return hashMap;
    }
    //判断密码是否正确
    public String judgePassword(String account, String password){
        HashMap<String, Account> hashMap = obtainAllAccount();
        for(Map.Entry<String, Account> map : hashMap.entrySet()){
            if(map.getKey().equals(account)){
                if(map.getValue().password.equals(password)){
                    return map.getValue().type;
                }
                else{
                    return null;
                }
            }
        }
        return null;
    }
    //根据账号获取id
    public String obtainId(String account){
        HashMap<String, Account> hashMap = obtainAllAccount();
        return hashMap.get(account).id;
    }
    //判断账号是否存在
    public boolean accountIsExist(String account){
        HashMap<String, Account> hashMap = obtainAllAccount();
        for(Map.Entry<String, Account> map : hashMap.entrySet()){
            if(map.getKey().equals(account))
                return true;
        }
        return false;
    }
    //判断密码是否存在
    public boolean passwordIsExist(String password){
        HashMap<String, Account> hashMap = obtainAllAccount();
        for(Map.Entry<String, Account> map : hashMap.entrySet()){
            if(map.getValue().password.equals(password))
                return true;
        }
        return false;
    }
    //注册账户
    public void enrollAccount(Account storageAccount) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonString = objectMapper.writeValueAsString(storageAccount);
        System.out.println(jsonString);
        String file = "src/main/java/org/example/ttms/file/account.txt";
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
            writer.write(jsonString+"\n");
//            writer.flush();
            writer.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //返回账号的类型
    public String accountType(String account){
        HashMap<String, Account> hashMap = obtainAllAccount();
        return hashMap.get(account).type;
    }
}
